/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "se_hisysevent.h"
#include "hisysevent.h"
#include "loghelper.h"
#include "se_common_exception.h"

namespace OHOS {
namespace SE {
template<typename... Types>
static void WriteEvent(const std::string& eventType, HiviewDFX::HiSysEvent::EventType type, Types... args)
{
    int ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SECURE_ELEMENT, eventType, type, args...);
    if (ret != 0) {
        ErrorLog("Write event fail: %{public}s", eventType.c_str());
    }
}

void SeHisysEvent::WriteSeFailedHiSysEvent(ChannelType channelType, ErrorCode errorCode)
{
    WriteEvent("ACCESS_SE_FAILED", HiviewDFX::HiSysEvent::EventType::FAULT,
               "CHANNEL_TYPE", channelType,
               "ERROR_CODE", errorCode);
    // also add a failure statistic count.
    WriteOpenChannelHiSysEvent(DEFAULT_COUNT, DEFAULT_COUNT);
}

void SeHisysEvent::WriteOpenChannelHiSysEvent(int requestCnt, int failCnt)
{
    WriteEvent("OPEN_CHANNEL", HiviewDFX::HiSysEvent::EventType::STATISTIC,
               "REQUEST_CNT", requestCnt,
               "FAILED_CNT", failCnt);
}

void SeHisysEvent::WriteSeResponseHiSysEvent(const std::string& response)
{
    if (response.length() < TARGET_LENGTH) {
        WriteEvent("RESPONSE_EXCEPTION", HiviewDFX::HiSysEvent::EventType::FAULT,
                   "EXCEPTION_VALUE", response);
    } else {
        std::string subRsp = response.substr(response.length() - TARGET_LENGTH, TARGET_LENGTH);
        WriteEvent("RESPONSE_EXCEPTION", HiviewDFX::HiSysEvent::EventType::FAULT,
                   "EXCEPTION_VALUE", subRsp);
    }
}

void SeHisysEvent::WriteSeResponseHiSysEvent(const std::string& command, const std::string& response)
{
    std::string rspStr = "";
    if (response.length() < TARGET_LENGTH) {
        rspStr = response;
    } else {
        rspStr = response.substr(response.length() - TARGET_LENGTH, TARGET_LENGTH);
    }
    if (rspStr == CORRECT_RESPONSE) {
        DebugLog("response success, no need to send hisysevent.");
        return;
    }

    std::string cmdStr = "";
    if (command.length() < TARGET_LENGTH) {
        cmdStr = command;
    } else {
        cmdStr = command.substr(0, TARGET_LENGTH);
    }

    InfoLog("error cmd[%{public}s], rsp[%{public}s]", cmdStr.c_str(), rspStr.c_str());
    WriteEvent("RESPONSE_EXCEPTION", HiviewDFX::HiSysEvent::EventType::FAULT,
               "EXCEPTION_VALUE", cmdStr + "|" + rspStr);
}
}  // namespace SE
}  // namespace OHOS